<?php


namespace App\Http\Controllers\Warehousing;


use App\Http\Controllers\Controller;
use App\Http\Requests\Warehousing\Tally\StoreDetailRequest;
use App\Http\Searchs\Warehousing\TallySearch;
use App\Repositories\Tally;
use Illuminate\Support\Facades\DB;

class TallyController extends Controller
{
    /**
     * @var Tally
     * @author luffyzhao@vip.126.com
     */
    private $tally;

    /**
     * TallyController constructor.
     * @param Tally $tally
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Tally $tally)
    {
        $this->tally = $tally;
    }

    /**
     * @param TallySearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(TallySearch $search){
        return $this->response(
            $this->tally->paginate($search->toArray())
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function show($id){
        return $this->response(
            $this->tally->show($id)
        );
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function confirm($id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->tally->confirm($id);
            })
        );
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function complete($id)
    {
        return $this->response(
            DB::transaction(/**
             * @author luffyzhao@vip.126.com
             */ function () use ($id) {
                $this->tally->complete($id);
            })
        );
    }

    /**
     * @param StoreDetailRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function storeDetail(StoreDetailRequest $request, $id){
        return $this->response(
            DB::transaction(function () use ($id, $request){
                $this->tally->storeDetail($id, $request->only([
                    'barcode',
                    'quantity',
                    'batch',
                    'attribute'
                ]));
            })
        );
    }

    /**
     * @param $id
     * @param $detailId
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function destroyDetail($id, $detailId){
        return $this->response(
            DB::transaction(function () use ($id, $detailId){
                $this->tally->destroyDetail($id, $detailId);
            })
        );
    }
}